﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TestBytes
{
    class Program
    {
        static void Main(string[] args)
        {
            Random r = new Random();
            byte[] b1 = new byte[1900 * 800 * 4];
            byte[] b2 = new byte[1900 * 800 * 4];
            byte[] b3 = new byte[1900 * 800 * 4];
            for (int index = 0; index < b1.Length; index++)
            {
                if (index % 5 == 0
                    || index % 6 == 0
                    || index % 7 == 0)
                {
                    b1[index] = (byte)(r.Next(0, 250));
                    b2[index] = b1[index];
                }
                else
                {
                    b1[index] = (byte)(r.Next(0, 250));
                    b2[index] = (byte)(r.Next(0, 250));
                }
            }

            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start();
            for (int index = 0; index < b1.Length; index++)
            {
                b3[index] = (byte)((b2[index] ^ b1[index]));
                if (b3[index] != 0)
                {
                    b3[index] = b2[index];
                }
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            byte[] b4 = GZip.Compress(b3);
            stopwatch.Stop();
            Console.WriteLine(stopwatch.ElapsedMilliseconds);

            Console.ReadKey();
        }
        /// <summary>
        /// 压缩数据
        /// </summary>
        public class GZip
        {
            /// <summary>
            /// 将字节数组进行压缩后返回压缩的字节数组
            /// </summary>
            /// <param name="data">需要压缩的数组</param>
            /// <returns>压缩后的数组</returns>
            public static byte[] Compress(byte[] data)
            {
                MemoryStream stream = new MemoryStream();
                GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress);
                gZipStream.Write(data, 0, data.Length);
                gZipStream.Close();
                return stream.ToArray();
            }

            /// <summary>
            /// 解压字符数组
            /// </summary>
            /// <param name="data">压缩的数组</param>
            /// <returns>解压后的数组</returns>
            public static byte[] Decompress(byte[] data)
            {
                MemoryStream stream = new MemoryStream();

                GZipStream gZipStream = new GZipStream(new MemoryStream(data), CompressionMode.Decompress);

                byte[] bytes = new byte[40960];
                int n;
                while ((n = gZipStream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    stream.Write(bytes, 0, n);
                }
                gZipStream.Close();
                return stream.ToArray();
            }
        }
    }
}
